﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public partial class _Contacts : System.Web.UI.Page 
{
    protected LocalFunctions FUNC;
    private int iGalleryID;
    protected string sTitle;
    protected string sKeywords, sDescription;

    protected void Page_Load(object sender, EventArgs e)
    {
        FUNC = ((Rondo)Master).FUNC;
        
        GetParameters();

        Dal.arParams.Clear();
        Dal.arParams.Add(FUNC.LangID);
        Dal.arParams.Add(iGalleryID);
        Dal.arParams.Add(1);
        DataSet DS=Dal.ExecuteDBspDS("RD_GetVideoGalleryByID", Dal.arParams);
        if (DS != null)
        {
            if (DS.Tables[0] != null)
            {
                foreach (DataRow DR in DS.Tables[0].Rows)
                {
                    int iTmpID = Convert.ToInt32(DR["id"]);

                    if (iTmpID < iGalleryID)
                    {
                        lnkPrevGal.Text = FUNC.GetPhrase(40);
                        lnkPrevGal.NavigateUrl = "videoGallery.aspx?gid=" + iTmpID;
                    }
                    else
                        if (iTmpID == iGalleryID)
                        {
                            sTitle = DR["Title"].ToString();
                            Title = sTitle;
                        }
                        else
                            if (iTmpID > iGalleryID)
                            {
                                lnkNextGal.Text = FUNC.GetPhrase(41);
                                lnkNextGal.NavigateUrl = "videoGallery.aspx?gid=" + iTmpID;
                            }
                }
            }
            if (DS.Tables[1] != null)
            {
                dlVideos.DataSource = DS.Tables[1];
                dlVideos.DataBind();
            }
        }
    }

    private void GetParameters()
    {
        iGalleryID = Convert.ToInt32(Request.QueryString["gId"]);
    }

    protected void dlVideos_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        string sLink= ((HiddenField)e.Item.FindControl("hfLink")).Value;

        HtmlAnchor htmlLnk1 = (HtmlAnchor)e.Item.FindControl("lnk1");
        HtmlAnchor htmlLnk2 = (HtmlAnchor)e.Item.FindControl("lnk2");
        Regex regex=new Regex(@"v\=([\-\w]+)");
        
        Match m=regex.Match(sLink);
        string mValue = m.Value.Replace("v=", "");
        sLink = string.Format("http://www.youtube.com/v/{0}&hl=en&fs=1&autoplay=1", mValue);
        htmlLnk1.HRef= sLink;
        htmlLnk2.HRef=sLink;

        string sImageLink = string.Format("../Thumbnails/Thumbnail.ashx?img=http://img.youtube.com/vi/{0}/0.jpg&sizeW=120&sizeH=90", mValue);

        HtmlImage htmImg = (HtmlImage)e.Item.FindControl("img1");
        htmImg.Src = sImageLink;
    }
}
